﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MatchCore;
using Net.Messgae;

public class UIBulletController : MonoBehaviour,IBulletEventer
{
    Bullet _bullet;
    bool _isOver = false;
    private float _speed = 1.0f;

    public void SetBullet(Bullet bullet)
    {
        _bullet = bullet;
        _bullet.SetBulletEventer(this);

        _speed = Config.GAME_PER_SEC_FRAME_NUM * Math.Abs(_bullet.GetSpeed())
            * UITransformHelper.GetUISceneSize().x / Config.SCENE_SIZE.x;
    }

    void Update()
    {
        if(_bullet != null)
        {
            if(_isOver)
            {
                OnOnver();
            }
            else
            {
                UnityEngine.Vector3 pos = UITransformHelper.Match2UIPosition(_bullet.GetPosition());
                UnityEngine.Vector3 sub = pos - transform.position;
                if(Math.Abs(sub.x) >= 1 || Math.Abs(sub.y) >= 1)
                {
                    try
                    {
                        // transform.position = transform.position 
                        // + new UnityEngine.Vector3(sub.x  * Time.deltaTime,sub.y * Time.deltaTime,0);

                        transform.position = pos;
                    }
                    catch(Exception ex)
                    {
                        Debug.LogError("Bullet Update Exception:" + ex);
                    }
                }
            }
        }
    }

    void OnOnver()
    {
        _bullet = null;

        Destroy(gameObject);
    }

    public void OnBulletHitPlayer(Bullet bullet,Player player)
    {
        _isOver = true;
    }

    public void OnBulletDestroy(Bullet bullet)
    {
        _isOver = true;
    }
}
